

import org.assertj.core.util.Lists;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

/**
 * @author <a href="mailto:enjie.xu@yunhutech.com">xiaoming</a>
 * @since 2021/6/11
 */
public class Tests {

    private static int count = 1;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread(()->{
            method1(10);
        },"t1").start();
//        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
//            @Override
//            public Integer call() throws Exception {
//                System.out.println("abc");
//                return 100;
//            }
//        });
//        new Thread(task,"t1").start();
//        System.out.println(task.get());
        Thread.sleep(200);
        method1(20);
    }

    private static void method1(int x) {
        int y = x + 1;
        count++;
        System.out.println(y);
        System.out.println("count "+count+" thread "+Thread.currentThread().getName());
    }


    @Test
    public void test4(){
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");

        List<String> list1 = list.stream().filter(x->
            Objects.equals(x, "b")
        ).collect(Collectors.toList());

        System.out.println("------------------");

        list1.forEach(x->{
            System.out.println(x);
        });


//        List<String> newList = Optional.ofNullable(list).orElse(Lists.newArrayList());
//        newList.forEach(x -> System.out.println(x));

    }



    @Test
    public void test(){
        Map<Integer,Integer> map = new HashMap<>();

        map.put(4,1);
        map.put(5,3);
        map.put(4,0);
        map.put(6,2);
        map.put(22,12);


        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, Integer> entry = iterator.next();
            int key = entry.getKey();
            int value = entry.getValue();

            int index = 0;
            try {
                index = key/value;
            } catch (Exception e) {
                System.out.println("有异常");
                throw new RuntimeException("dd");
            }
            System.out.println("key "+entry.getKey()+" value "+entry.getValue());
            System.out.println("index "+index);
        }

    }


    /**
     * +=操作符会进行隐式自动类型转换，此处a+=b隐式的将加操作的结果类型强制转换为持有结果的类型
     * 而 a=a+b则不会自动进行类型转换
     */
    @Test
    public void test1(){
        byte a = 127;
        byte b = 127;

        byte c = 2;
        byte d = 4;
        int res = a + b;
        d = (byte) (c + d);
    }


    /**
     * short类型在进行运算时会自动提升为int类型，也就是说s+1的运算结果是int类型，而s是short类型，此时编译器会报错
     * 而+=操作符会对右边的表达式结果强转匹配左边的数据类型
     */
    @Test
    public void test2(){
        short s = 1;
//        s = s+1;
        s+=1;
    }

    @Test
    public void test3(){
        final Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1");
            }
        });

        final Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t2");
            }
        });

        final Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t3");
            }
        });


        t1.start();
        t2.start();
        t3.start();
    }


    @Test
    public void test6(){
        List<Integer> list = new ArrayList<>();
        Integer i = new Integer(2);
        Optional.ofNullable(i).ifPresent(t-> {
            System.out.println("t="+t);
            t = 3;
            list.add(t);

        });

        System.out.println(i);
        System.out.println(list);
    }


    @Test
    public void test7(){
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");

        Optional<String> optional = Optional.ofNullable(list).orElse(Lists.newArrayList()).stream().filter(res -> Objects.equals("c", res)).findFirst();
        Optional.ofNullable(optional.get()).orElseThrow(RuntimeException::new);
    }
}
